{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demo(a, b , c):\n",
    "    print(a, b, c)\n",
    "\n",
    "demo(c = 9, a = 7 , b = 8)\n",
    "demo(7, c = 9, b = 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "def demo(a, b , c):\n",
    "    print(a, b, c)\n",
    "'''\n",
    "a_list = [1, 2, 3]\n",
    "a_tuple = (5, 6, 7)\n",
    "a_set = {8, 9, 10}\n",
    "a_dict = {'a':11,'c':13,'b':12}\n",
    "\n",
    "demo(*a_list)\n",
    "demo(*a_tuple)\n",
    "demo(*a_set)\n",
    "demo(*a_dict)\n",
    "demo(*a_dict.keys())\n",
    "demo(*a_dict.values())\n",
    "demo(**a_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_dict = {'a':11,'b':12,'c':13}\n",
    "demo(*a_dict)\n",
    "demo(*a_dict.keys())\n",
    "demo(*a_dict.values())\n",
    "demo(**a_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = \"global object\"\n",
    "\n",
    "def demo_scope():\n",
    "    x = 10\n",
    "    print(x)\n",
    "    global x\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demo_scope2():\n",
    "    y = 10\n",
    "    id(y)\n",
    "    global y\n",
    "    id(y)\n",
    "\n",
    "demo_scope2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "global object\n",
      "oh...another global object\n"
     ]
    }
   ],
   "source": [
    "y = \"global object\"\n",
    "def demo_scope3():\n",
    "    y = 10\n",
    "    global another_global\n",
    "    another_global = \"oh...another global object\"\n",
    "    print(y)\n",
    "\n",
    "# print(another_global) # it would be ok...why?\n",
    "demo_scope3()\n",
    "print(y)\n",
    "print(another_global)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "1 2 3\n",
      "[11, 12, 13, 14, 15]\n"
     ]
    }
   ],
   "source": [
    "f = lambda x, y, z: print(x, y, z)\n",
    "f(1, 2, 3)\n",
    "f(1, z = 3, y = 2)\n",
    "\n",
    "l = [1, 2, 3, 4, 5]\n",
    "print(list(map(lambda x: x + 10, l)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "10\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "    print(20)\n",
    "print(10)\n",
    "print(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1 1 1 1 1 1 1 1 1 \n",
      "\n",
      "140514826626128\n",
      "140514826532336\n",
      "1 1 2 3 5 8 13 21 34 55 "
     ]
    }
   ],
   "source": [
    "def f():\n",
    "    a, b = 1, 1\n",
    "    while True:\n",
    "        yield a\n",
    "        a, b = b, a + b\n",
    "\n",
    "for i in range(10):\n",
    "    print(f().__next__(), end = ' ')\n",
    "\n",
    "print('\\n')\n",
    "\n",
    "a = f()\n",
    "print(id(a))\n",
    "print(id(f))\n",
    "for i in range(10):\n",
    "    print(a.__next__(), end = ' ')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
